
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
import 'package:star_unbounded/model/clipboard_data.dart';
import 'package:star_unbounded/pages/index/widget/picker_image_button.dart';
import 'package:star_unbounded/pages/index/widget/text_input.dart';

class BottomBar extends StatefulWidget {
  /// 底部输入框
  const BottomBar({super.key});

  @override
  State<BottomBar> createState() => _BottomBarState();
}

class _BottomBarState extends State<BottomBar>  {


  final TextEditingController _textEditingController = TextEditingController();



  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }


  void onInput(String text){
    print(text);
  }

  void onPaste() async {
    if(!mounted)return;
    var text = await ClipboardData.readText();
    if(text != null && text != ""){
      _textEditingController.text = text;
      return;
    }
    var img = await ClipboardData.readImg();
    _showImageDialog(img);

  }

  void onSend(dynamic payload) async {
    // 这个地方对接接口
    if(payload == null){
      return;
    }

    if(payload is String){
      print("发送的字符串$payload");
    }

    if(payload is XFile){
      payload = await payload.readAsBytes();
    }

    if(payload is Uint8List){
      print("文件大小：${payload.length}");
    }
  }


  void _showImageDialog(dynamic img) async {
    if(!mounted || img == null)return;
    var imgWidget;
    double width = 320;
    double height = 320;
    BoxFit fit = BoxFit.contain;
    if(img is Uint8List){
      imgWidget = Image.memory(
        img,
        width:width,
        height:height,
        fit:fit,
      );
    }

    if(img is XFile && kIsWeb){

      img = await img.readAsBytes();
      imgWidget = Image.memory(
        img,
        width:width,
        height:height,
        fit:fit,
      );

    }
    if(img is XFile && !kIsWasm){
      imgWidget = Image.file(File(img.path),
        width:width,
        height:height,
        fit:fit
      );
    }

    showDialog(
        context: context,
        builder: (context){
          return  Center(
            child: Card(
              child: Container(
                padding: const EdgeInsets.all(12),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    imgWidget,
                    const SizedBox(height: 6),
                    IconButton.filledTonal(onPressed: (){
                      onSend(img);
                      Navigator.pop(context);
                    }, icon: const Icon(Icons.send))
                  ],
                ),
              ),
            ),
          );
        });
  }



  @override
  Widget build(BuildContext context) {

    return Container(
      color: Theme.of(context).colorScheme.inversePrimary,
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 12,right: 12,),
            child: Row(
              children: [
                 PickerImageButton(
                  onChange: _showImageDialog,
                ),
                IconButton(onPressed: onPaste, icon: const Icon(Icons.paste_outlined))
              ],
            ),
          ),
          Row(
            children: [
              Expanded(child: TextInput(
                textEditingController: _textEditingController,
                onInput:onInput,
                context: context,
              ),),
              IconButton(onPressed: ()=>onSend(_textEditingController.text), icon: const Icon(Icons.send)),
            ],
          ),
          SizedBox(height: MediaQuery.of(context).padding.bottom,)
        ],
      ),
    );
  }
}
